package com.Action;

import com.Entity.AccountEntity;
import com.Service.DAO.AccountDAO;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

import java.util.List;
import java.util.Map;

public class AccountAction implements RequestAware {
    private String id;
    private String name;
    private String password;
    private String newPassword1;
    private String newPassword2;
    private Map<String, Object> request;
    List<AccountEntity> accountEntityList;

    public String getNewPassword1() {
        return newPassword1;
    }

    public void setNewPassword1(String newPassword1) {
        this.newPassword1 = newPassword1;
    }

    public String getNewPassword2() {
        return newPassword2;
    }

    public void setNewPassword2(String newPassword2) {
        this.newPassword2 = newPassword2;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String Login() {
        AccountDAO accountDao = new AccountDAO();
        AccountEntity account = new AccountEntity(Integer.parseInt(id),password);
        AccountEntity ret = accountDao.get(account);
        if (ret != null&&ret.getPassword().equals(password)) {
            if(ret.getType() == 0) return "root";
            else {
                ActionContext.getContext().put("name", ret.getName());
                return "success";
            }
        } else
            return "error";
    }

    public String Search() {
        AccountDAO accountDao = new AccountDAO();
        accountEntityList = accountDao.Search(name);
        ActionContext.getContext().put("list", accountEntityList);
        return "list";
    }



    public String Register() {
        AccountDAO accountDao = new AccountDAO();
        AccountEntity account = new AccountEntity(Integer.parseInt(id),password,name,1);
        if(accountDao.Register(account)){
            return "success";
        }else
            return "error";
    }

    public String GetById() {
        AccountDAO accountDao = new AccountDAO();
        AccountEntity account = new AccountEntity(Integer.parseInt(id),password);
        AccountEntity ret = accountDao.get(account);
        ActionContext.getContext().put("name", ret.getName());
        return "success";
    }

    public String AccountList(){
        AccountDAO accountDao = new AccountDAO();
        accountEntityList = accountDao.getAll();
        ActionContext.getContext().put("list", accountEntityList);
        return "list";
    }

    public String Delect(){
        AccountDAO accountDao = new AccountDAO();
        AccountEntity account = new AccountEntity(Integer.parseInt(id),password,name,1);
        if(accountDao.delete(account)){
            return "success";
        }else
            return "error";
    }

    public String Update(){
        AccountDAO accountDao = new AccountDAO();
        AccountEntity account = new AccountEntity(Integer.parseInt(id));
        AccountEntity ret = accountDao.get(account);
        ret.setName(name);
        if(accountDao.update(ret)){
            return "success";
        }else
            return "error";
    }

    public String ChangPw(){
        AccountDAO accountDao = new AccountDAO();
        AccountEntity account = new AccountEntity(Integer.parseInt(id));
        AccountEntity ret = accountDao.get(account);
        ActionContext.getContext().put("name", ret.getName());
        if(ret.getPassword().equals(password)&&newPassword1.equals(newPassword2)){
            ret.setPassword(newPassword1);
            accountDao.update(ret);
            return "success";
        }
        else
            return "error";
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        // TODO Auto-generated method stub
        this.request = request;
    }
}
